/*
 * Copyright (c) 2023-2025 elsfs Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.elsfs.cloud.common.mybatis.ext.sql.support.executor;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.elsfs.cloud.common.mybatis.ext.db.GenericSqlExecutor;
import org.elsfs.cloud.common.mybatis.ext.sql.SqlExecutor;
import org.elsfs.cloud.common.mybatis.ext.sql.support.builder.GenericSelectSql;

/**
 * 查询分页数据SQL执行器
 *
 * @param <R> 结果类型
 */
@RequiredArgsConstructor
public class QueryPageSqlExecutor<R> extends GenericSelectSql<QueryPageSqlExecutor<R>>
    implements SqlExecutor<IPage<R>> {

  /** 通用SQL执行器 */
  private final GenericSqlExecutor genericSqlExecutor;

  /** 分页对象 */
  private final IPage<R> pageable;

  /** 结果类 */
  private final Class<R> clazz;

  /**
   * 执行SQL
   *
   * @return 结果
   */
  @Override
  public IPage<R> execute() {
    return this.genericSqlExecutor.selectPage(
        this.buildSqlFragment(),
        pageable,
        this.getSqlParameterManager().getSqlParameters(),
        this.clazz);
  }
}
